<?php
    // build to generate a random charcode
  function zen_random_charcode($length) {
	    $arraysize = 34;
	    $chars = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9');

	    $code = '';
		for ($i = 1; $i <= $length; $i++) {
		$j = floor(zen_rand(0,$arraysize));
		$code .= $chars[$j];
		}
		return  $code;
 }

 function zen_display_vvcodes(){
   $visual_verify_code = zen_random_charcode(6);
   $_SESSION['vvcode'] = $visual_verify_code;
   return $visual_verify_code;
 }
 function zen_page_path_check($page,$pages_check){
     return in_array($page,$pages_check);
 }
 ////
 function zen_get_top_categories($categories_array = '', $parent_id = '0', $indent = '', $status_setting = '') {
    global $db;

    if (!is_array($categories_array)) $categories_array = array();

    // show based on status
    if ($status_setting != '') {
      $zc_status = " c.categories_status='" . (int)$status_setting . "' and ";
    } else {
      $zc_status = '';
    }

    $categories_query = "select c.categories_id, cd.categories_name, c.categories_status
                         from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                         where " . $zc_status . "
                         parent_id = '" . (int)$parent_id . "'
                         and c.categories_id = cd.categories_id
                         and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                         order by sort_order, cd.categories_name";

    $categories = $db->Execute($categories_query);

    while (!$categories->EOF) {
      $categories_array[] = array('id' => $categories->fields['categories_id'],
                                  'text' => $indent . $categories->fields['categories_name']);
      #############Begin:just limit for top category########
	  //modified by john 2010-08-18
      /*if ($categories->fields['categories_id'] != $parent_id) {
        $categories_array = zen_get_categories($categories_array, $categories->fields['categories_id'], $indent . '&nbsp;&nbsp;', '1');
      }*/
	  #############End:just limit for top category #########
      $categories->MoveNext();
    }

    return $categories_array;
  }
////
function make_link_categories(&$link_categories,$categories){
	global $last_element;
	foreach($categories as $cid=>$category){

		if(isset($last_element)){
			$link_categories[$last_element]['next_one']=$cid;
		}
 
		$link_categories[$cid]=array('category_name'=>$category['category_name'],
									 'category_url_link'=>$category['category_url_link'],
									 'categories_image'=>$category['category_image'],
									 'c_level'=>$category['c_level'],
									 'parent_id'=>$category['parent_id'],
									 'has_sub_cate'=>false,
									 'sub_count'=>0,
									 'last_one'=>isset($last_element)?$last_element:0,
									 'next_one'=>false
									);
		$last_element=$cid;
		$sub_count=@count($category['sub_categories']);
		if(!empty($category['has_sub_cate']) && $sub_count>0 ){
				$link_categories[$cid]['has_sub_cate']=true;
				$link_categories[$cid]['sub_count']=$sub_count;
				make_link_categories($link_categories,$category['sub_categories']);
		}
	}
}

function make_esub_categories(&$sub_html_content,$cate_id,$end_level){
	global $link_categories,$sub_html_content,$hh_parent_id;

	$next_id=$link_categories[$cate_id]['next_one'];
	$categories_name_length = strlen($link_categories[$cate_id]['category_name']);
	if($categories_name_length >= 40){
	       $categories_name = substr($link_categories[$cate_id]['category_name'],0,40).'...';
	}else{
	       $categories_name = $link_categories[$cate_id]['category_name'];
	}
	if($link_categories[$cate_id]['c_level']==2){
		$sub_html_content.='<dl class="'.(($cate_id==$hh_parent_id)?'menu1':'menu2').'">'.'<dt class="'.(($cate_id==$hh_parent_id)?'':'menu2_title').'">
		<a href="'.$link_categories[$cate_id]['category_url_link'].'" title="'.$link_categories[$cate_id]['category_name'].'">'.$categories_name.'</a></dt>';

	}else{
		$sub_html_content.=' <dd><a href="'.$link_categories[$cate_id]['category_url_link'].'" title="'.$link_categories[$cate_id]['category_name'].'">'.$categories_name.'</a></dd>';
	}

	if(isset($link_categories[$next_id])&&$link_categories[$next_id]['c_level']!=$end_level){

		if($link_categories[$next_id]['c_level']==2){
			$sub_html_content.='</dl>';
		}
		make_esub_categories($sub_html_content,$next_id,$end_level);

	}else{
		$sub_html_content.='</dl>';
	}
}

//add du 2011.9.07 star

//add by zhengcongzhen 20111008 begin
function zen_get_level_one_category_id($category_id)
{
	global $db;
	$get_level_one_category_id_sql='SELECT `parent_id` FROM `categories` WHERE `categories_id`='.(int)$category_id;
	$get_level_one_category_id_result=$db->Execute($get_level_one_category_id_sql);
	$tem_parent_id=$get_level_one_category_id_result->fields['parent_id'];
	if(0 == $tem_parent_id)
	{
		return $category_id;
	}else{
		return zen_get_level_one_category_id($tem_parent_id);
	}
}
//add by zhengcongzhen 20111008 end

function return_categories_products($categories_id,$num)
{
	global $db;
	if(is_array($categories_id))
	{
		$categories_id = implode(',',$categories_id);
	}

	$sql = "SELECT `products_id`,`products_ordered` FROM `products` WHERE products_status = 1 and `products_id` in(select products_id from products_to_categories where categories_id in(".$categories_id.")) order by products_ordered DESC limit ".$num;
	// echo $sql;die();
	$curr_cate_all_pro = $db->Execute($sql);
	$curr_cate_all_pro_arr = array();
	while (!$curr_cate_all_pro->EOF)
	{
		$new_curr_cate_all_pro_arr[] = $curr_cate_all_pro->fields['products_id'];

		$curr_cate_all_pro->MoveNext();
	}
	return $new_curr_cate_all_pro_arr;
}


	function get_category_name($category_id)
	{
		global $db;
		$language_id = 2;

		$query = "select categories_name from categories_description where categories_id=\"$category_id\" and language_id=\"$language_id\"";
		$result = $db->Execute($query);
		$category_name = $result->fields['categories_name'];

		return $category_name;
    }

	function get_subcategories(&$subcategories_array, $parent_id = 0)
	{
		global $db;
		$subcategories_query = "select categories_id
								from " . TABLE_CATEGORIES . "
								where parent_id = '" . (int)$parent_id . "'
								  and categories_status = 1";

		$subcategories = $db->Execute($subcategories_query);

		while (!$subcategories->EOF) {
		  $subcategories_array[sizeof($subcategories_array)] = $subcategories->fields['categories_id'];
		  $subcategories->MoveNext();
		}
	}

	function get_categories_arr()
	{
		$categories_arr = array();
		$level_1_categories = array();
		 get_subcategories($level_1_categories, 0);
		 foreach ($level_1_categories as $level_1_ctg)
		 {
			$categories_arr[$level_1_ctg] = $level_1_ctg;
			$categories_arr[$level_1_ctg . '_name'] = get_category_name($level_1_ctg);

			$sub_categories = array();
			zen_get_subcategories($sub_categories, $level_1_ctg);
			foreach ($sub_categories as $sub_ctg)
			{
				$categories_arr[$sub_ctg] = $level_1_ctg;
			}
		 }

		 return $categories_arr;
	}
         function products_id_get_level_one_category_id($products_id)
         {
             global $db;
             $sql="select categories_id from products_to_categories where products_id='$products_id' limit 1";
             $pid=$db->Execute($sql);
             return zen_get_level_one_category_id($pid->fields['categories_id']);
         }

function zen_get_store_timezone($country) { //add by wxf 20120320
		switch($country) {
		case 'Germany':
			return 'Europe/Berlin';
		    break;
		case 'France':
			return 'Europe/Paris';
			break;
		case 'Italy':
			return 'Europe/Rome';
			break;
		case 'Portugal':
			return 'Europe/Lisbon';
		    break;
		case 'Spain':
			return 'Europe/Madrid';
		    break;
		case 'Russian Federation':
			return 'Europe/Moscow';
		    break;
		case 'Japan':
			return 'Asia/Tokyo';
		    break;
		}
}

?>
